package cn.zb.zbrhback.org.controller;

import cn.zb.zbrhback.basic.util.AjaxResult;
import cn.zb.zbrhback.org.domain.PageQuery;
import cn.zb.zbrhback.basic.util.PageResult;
import cn.zb.zbrhback.org.domain.Employee;
import cn.zb.zbrhback.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@Api(value = "员工Api",tags = {"员工操作接口"})
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    /**
     * 新增或修改
     * */
    @ApiOperation(value = "新增或修改接口",notes = "有id为修改，否则为新增")
    @PutMapping
    public AjaxResult addorUpdate(@RequestBody Employee employee){
        try {
            if(Objects.nonNull(employee.getId())){
                employeeService.update(employee);
            }else {
                employeeService.add(employee);
            }
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("编辑失败"+e.getMessage());
        }
    }

    /**
     * 根据id删除
     * */
    @ApiOperation(value = "删除接口",notes = "根据主键id删除")
    @DeleteMapping("/{id}")
    public AjaxResult deleteById(@ApiParam(name = "id",value = "删除的主键必须",required = true) @PathVariable("id")Long id){
        try {
            employeeService.deleteById(id);
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败"+e.getMessage());
        }
    }

    /**
     * 根据id查询
     * */
    @ApiOperation(value = "单个查询接口",notes = "根据id查询")
    @GetMapping("/{id}")
    public AjaxResult getById(@ApiParam(name = "id",value = "查询的主键必须",required = true) @PathVariable("id")Long id){
        try {
            Employee employee = employeeService.getById(id);
            return AjaxResult.me().setData(employee);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询失败"+e.getMessage());
        }
    }

    /**
     * 查询所有
     * */
    @ApiOperation(value = "查询所有接口",notes = "不需要任何参数")
    @GetMapping
    public AjaxResult getAll(){
        try {
            List<Employee> employees = employeeService.getAll();
            return AjaxResult.me().setData(employees);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询失败"+e.getMessage());
        }
    }

    /**
     * 分页条件查询
     * */
    @ApiOperation(value = "分页条件查询接口",notes = "需要参数当前页以及每页显示条数，条件可有可无")
    @PostMapping
    public AjaxResult findPage(@RequestBody PageQuery pageQuery){
        try {
            System.out.println("请求");
            PageResult pagemsg = employeeService.findPage(pageQuery);
            System.out.println("分页条件查询数据"+pagemsg);
            return AjaxResult.me().setData(pagemsg);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("分页条件查询失败"+e.getMessage());
        }
    }

    @ApiOperation(value = "批量删除接口",notes = "需要参数删除的id数组")
    @PatchMapping
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        try {
            employeeService.batchDelete(ids);
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("分页条件查询失败"+e.getMessage());
        }
    }
}
